package Ffmpeg

import (
	"Intelligence/Util/Public"
	"errors"
	"fmt"
	"os"
	"strconv"
	"strings"
)

//是否开启日记
func _ConfigOn()string  {
	if true {
		return ""
	}
	return " -loglevel quiet"
}

// 判断文件或目录是否存在
func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}

type Ffmpeg struct {}

//分割视频成图片
func (data Ffmpeg)FenGeShiPinChengTuPian(mp4,path string,s int)error {
	//cmdStr := fmt.Sprint("ffmpeg",_ConfigOn()," -ss 00:00 -i ",mp4," -f image2 -r ",s," -c copy ",path,"%8d.jpg")
	//cmdStr := fmt.Sprint("ffmpeg",_ConfigOn()," -ss 00:00 -i ",mp4," -f image2 -r ",s," -y -vcodec copy -acodec copy ",path,"%8d.jpg")
	//cmdStr := fmt.Sprint("ffmpeg",_ConfigOn()," -i ",mp4," -r ",s," -y -c copy ",path,"%8d.jpg")
	s64, err := strconv.ParseFloat(fmt.Sprintf("%.8f", float64(1)/float64(s)), 64)
	if err != nil {
		return errors.New("数据转换失败")
	}
	cmdStr := fmt.Sprint("ffmpeg",_ConfigOn()," -i ",mp4," -vf fps=",s64," -y ",path,"%8d.jpg")
	args := strings.Split(cmdStr, " ")
	msg, err := Public.Cmd(args[0], args[1:])
	if err != nil {
		return errors.New(fmt.Sprintf("videoMerge failed, %v, output: %v\n", err, msg))
	}
	return nil
}

//分割视频成视频
func (data Ffmpeg)Get视频分割视频(mp4,path,s string)error {
	cmdStr := fmt.Sprint("ffmpeg",_ConfigOn()," -ss 00:00 -i ",mp4," -f segment -segment_time ",s," -segment_format_options movflags=+faststart ",path,"%8d.mp4")
	args := strings.Split(cmdStr, " ")
	msg, err := Public.Cmd(args[0], args[1:])
	if err != nil {
		return errors.New(fmt.Sprintf("videoMerge failed, %v, output: %v\n", err, msg))
	}
	return nil
}

//执行自定义脚本文件 txt=脚本文件路径 mp4=生成文件路径
func (data Ffmpeg)ZhiXingZiDingYiMingLingWenJian(txt,mp4 string)error  {
	cmdStr := fmt.Sprint("ffmpeg",_ConfigOn()," -f concat -safe 0 -i ",txt," -y -c copy -vsync vfr -pix_fmt yuv420p ",mp4)
	//cmdStr := fmt.Sprint("ffmpeg",_ConfigOn()," -f concat -safe 0 -i ",txt," -y -vsync vfr -pix_fmt yuv420p ",mp4)
	args := strings.Split(cmdStr, " ")
	msg, err := Public.Cmd(args[0], args[1:])
	if err != nil {
		return errors.New(fmt.Sprintf("videoMerge failed, %v, output: %v\n", err, msg))
	}
	return nil
}

//视频合成音频 mp4=视频路径 mp3=音频路径 mp42=生成文件路径
func (data Ffmpeg)ShiPinHeChengYinPin(mp4,mp3,mp42 string)error  {
	cmdStr := fmt.Sprint("ffmpeg",_ConfigOn()," -i ",mp4," -i ",mp3," -y -vcodec libx264 -af apad -shortest ",mp42)
	//cmdStr := fmt.Sprint("ffmpeg",_ConfigOn()," -i ",mp4," -i ",mp3," -y -c copy ",mp42)
	args := strings.Split(cmdStr, " ")
	msg, err := Public.Cmd(args[0], args[1:])
	if err != nil {
		return errors.New(fmt.Sprintf("videoMerge failed, %v, output: %v\n", err, msg))
	}
	return nil
}

//mp4=视频路径 mp42=生成文件路径
func (data Ffmpeg)Get视频提取视频(mp4,mp42 string)error  {
	cmdStr := fmt.Sprint("ffmpeg",_ConfigOn()," -i ",mp4," -y -vcodec copy -an ",mp42)
	//cmdStr := fmt.Sprint("ffmpeg",_ConfigOn()," -i ",mp4," -y -vcodec copy -acodec copy ",mp42)
	args := strings.Split(cmdStr, " ")
	msg, err := Public.Cmd(args[0], args[1:])
	if err != nil {
		return errors.New(fmt.Sprintf("videoMerge failed, %v, output: %v\n", err, msg))
	}
	return nil
}

//mp4=视频路径 mp3=生成文件路径
func (data Ffmpeg)Get视频提取音频(mp4,mp3 string)error  {
	cmdStr := fmt.Sprint("ffmpeg",_ConfigOn()," -i ",mp4," -vn -y -acodec copy ",mp3)
	args := strings.Split(cmdStr, " ")
	msg, err := Public.Cmd(args[0], args[1:])
	if err != nil {
		return errors.New(fmt.Sprintf("videoMerge failed, %v, output: %v\n", err, msg))
	}
	return nil
}

//视频转换h.264编码 mp4=视频路径 mp42=生成文件路径
func (data Ffmpeg)Set视频转码H264(mp4,mp42 string)error {
	cmdStr := fmt.Sprint("ffmpeg", _ConfigOn(), " -i ", mp4, " -vcodec libx264 ",mp42)
	args := strings.Split(cmdStr, " ")
	msg, err := Public.Cmd(args[0], args[1:])
	if err != nil {
		return errors.New(fmt.Sprintf("videoMerge failed, %v, output: %v\n", err, msg))
	}
	return nil
}

//视频转换AVCA编码 mp4=视频路径 mp42=生成文件路径
func (data Ffmpeg)Set视频转码AVCA(mp4,mp42 string)error {
	cmdStr := fmt.Sprint("ffmpeg", _ConfigOn(), " -i ", mp4, " -vcodec AVCA ",mp42)
	args := strings.Split(cmdStr, " ")
	msg, err := Public.Cmd(args[0], args[1:])
	if err != nil {
		return errors.New(fmt.Sprintf("videoMerge failed, %v, output: %v\n", err, msg))
	}
	return nil
}